package com.example.myapplication.ui.home

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.addCallback
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.NavHostFragment
import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentHomeBinding
import com.xinnuo.delegate.viewBinding

class HomeFragment : Fragment(R.layout.fragment_home) {

    private lateinit var homeViewModel: HomeViewModel
    private val binding by viewBinding(FragmentHomeBinding::bind)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        homeViewModel.text.observe(viewLifecycleOwner, {
            binding.textHome.text = it
        })

        binding.textHome.setOnClickListener {
            NavHostFragment.findNavController(this)
                .navigate(R.id.action_navigation_home_to_firstFragment)
        }

        requireActivity()
            .onBackPressedDispatcher
            .addCallback(this) {
                Log.i("=============", "HomeFragment")
            }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i("=============", "HomeFragment:onDestroy")
    }

}